package com.ocean.courtyard.util;

import com.ocean.courtyard.constants.ConstantsAttribute;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;

public class FileUtils {

    public static String getServerName(FileConfiguration config) {
        return config.getString(ConstantsAttribute.SERVER_NAME);
    }

    /**
     * 获取服务器根目录下地图保存路径
     *
     * @param player player
     * @param plugin plugin
     * @return 路径
     */
    public static String getWorldPath(Player player, JavaPlugin plugin) {
        String dataFolder = getWorldContainer(plugin.getConfig());
        String worldName = dataFolder + File.separator + player.getName();
        worldName = worldName.replace("\\", "/");
        return worldName;
    }

    /**
     * 获取地图模板保存路径
     *
     * @param templateWorldName 模板名称
     * @param plugin            plugin
     * @return 路径
     */
    public static String getTemplateFile(String templateWorldName, JavaPlugin plugin) {
        String templateFolder = getTemplateFolder(plugin.getConfig());
        return plugin.getDataFolder() + File.separator + templateFolder + File.separator + templateWorldName;
    }

    /**
     * 获取根目录文件夹
     *
     * @param file FileConfiguration
     * @return 根目录文件夹名称
     */
    public static String getWorldContainer(FileConfiguration file) {
        return file.getString(ConstantsAttribute.WORLD_FOLDER, "courtyard");
    }

    /**
     * 获取插件文件夹
     *
     * @param file FileConfiguration
     * @return 插件文件夹名称
     */
    public static String getTemplateFolder(FileConfiguration file) {
        return file.getString(ConstantsAttribute.TEMPLATE_FOLDER, "template");
    }
}
